

import UIKit
@_exported import LXExtensions

public class LXLimitInput: NSObject {
    static let shared = LXLimitInput.init()
    public override init() {
        super.init()
    }
    
    public func work(){
        NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: nil, queue: OperationQueue.main) { (notify) in
            let textField = notify.object as? UITextField
            if textField == nil {
                return
            }
            if !(textField!.markedTextRange?.isEmpty ?? true) {
                return
            }
            if textField!.maxCount <= 0 || textField!.text?.count ?? 0 <= textField!.maxCount {
                return
            }
            textField!.text = textField?.text?.substring(toIndex: textField!.maxCount)

//            if textField?.regexFit?.isEmpty ?? true {
//                if textField!.maxCount <= 0 || textField!.text?.length ?? 0 <= textField!.maxCount {
//                    return
//                }
//                textField!.text = textField?.text?.substring(toIndex: textField!.maxCount)
//            }else{
//                guard let inputText = textField?.text else {return}
//                guard let pattern = textField?.regexFit else {return}
//                
//                let predicate = NSPredicate(format: "SELF matches %@", pattern)
//
//                let isFit : Bool = predicate.evaluate(with: inputText)
//                
//                if !isFit {
//                    
//                }
//            }
//            XLogDebug(textField!.text ?? "")
        }
        
        NotificationCenter.default.addObserver(forName: UITextView.textDidChangeNotification, object: nil, queue: OperationQueue.main) { (notify) in
            let textField = notify.object as? UITextView
            if textField == nil {
                return
            }
            if !(textField!.markedTextRange?.isEmpty ?? true) || textField!.maxCount <= 0 || textField!.text?.count ?? 0 <= textField!.maxCount {
                return
            }
            textField!.text = textField?.text?.substring(toIndex: textField!.maxCount)
            LXLog(textField!.text ?? "")
        }
    }
    
    func stop(){
        NotificationCenter.default.removeObserver(self, name: UITextField.textDidChangeNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: nil)

    }
}

private var UITextFieldMaxCountKey: String = "UITextFieldMaxCountKey"
private var UITextViewMaxCountKey: String = "UITextViewMaxCountKey"

public extension UITextField {
    var maxCount: Int {
        get {
            return (objc_getAssociatedObject(self, &UITextFieldMaxCountKey) as? Int ?? -1)
        }
        set(newValue) {
            objc_setAssociatedObject(self, &UITextFieldMaxCountKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
    }
    
//    var regexFit: String? {
//        get {
//            return (objc_getAssociatedObject(self, &UITextFieldRegexFitKey) as? String)
//        }
//        set(newValue) {
//            objc_setAssociatedObject(self, &UITextFieldRegexFitKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
//        }
//    }
    
    
}
public extension UITextView {
    var maxCount: Int {
        get {
            return (objc_getAssociatedObject(self, &UITextViewMaxCountKey) as? Int ?? -1)
        }
        set(newValue) {
            objc_setAssociatedObject(self, &UITextViewMaxCountKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
    }
    
//    var regexFit: String? {
//        get {
//            return (objc_getAssociatedObject(self, &UITextViewRegexFitKey) as? String)
//        }
//        set(newValue) {
//            objc_setAssociatedObject(self, &UITextViewRegexFitKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
//        }
//    }

}
